package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;
import com.tenton.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "UpdateUserPositionServlet",urlPatterns = "/updateUserPositionServlet")
public class UpdateUserPositionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //Id
        String sid = request.getParameter("id");
        String num =sid.replaceAll("\\D+", "");
        int id =Integer.parseInt(num);
        String positionName = request.getParameter("positionName");
        UserService userService = new UserServiceImpl();
        User user = userService.getUserById(id);
        user.setProvince(positionName);
        //修改用户
        userService.updateUser(user);
        request.getRequestDispatcher("/listUserServlet.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
